<?php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../src/cloud.php';

/*
 * A simple Slim based sample application
 *
 * See Slim documentation:
 * http://www.slimframework.com/docs/
 */

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Slim\Views\PhpRenderer;
use \LeanCloud\Client;
use \LeanCloud\Storage\CookieStorage;
use \LeanCloud\Engine\SlimEngine;
use \LeanCloud\Query;
use \LeanCloud\Object;

$app = new \Slim\App();
// 禁用 Slim 默认的 handler，使得错误栈被日志捕捉
unset($app->getContainer()['errorHandler']);

Client::initialize(getenv("LC_APP_ID"),getenv("LC_APP_KEY"),getenv("LC_APP_MASTER_KEY"));

//Client::initialize("Di3UPU0fwgl2tLvr2PBOXLi7-gzGzoHsz", "yOuoxQWaAa5aDGByHHBDeE51", "8WHrcHqgPRmLQHwWM6jJW1TT");

// 将 sessionToken 持久化到 cookie 中，以支持多实例共享会话
Client::setStorage(new CookieStorage());

SlimEngine::enableHttpsRedirect();
$app->add(new SlimEngine());

// 使用 Slim/PHP-View 作为模版引擎
$container = $app->getContainer();
$container["view"] = function($container) {
    return new \Slim\Views\PhpRenderer(__DIR__ . "/views/");
};

$app->get('/', function (Request $request, Response $response) {
	$token="yuanzh";
	$data=$request->getQueryParams();
	if(!empty($data["signature"])){
		$signature=$data["signature"];
		$timestamp=$data["timestamp"];
		$nonce=$data["nonce"];
		$echostr=$data["echostr"];
		$array = array($token, $timestamp, $nonce);
		sort($array, SORT_STRING);
		$shastr=sha1(implode($array));
		$todo = new Object("WxRequest");
		$todo->set("signature", $signature);
		$todo->set("timestamp", $timestamp);
		$todo->set("nonce", $nonce);
		$todo->set("echostr", $echostr);
		$todo->set("shastr", $shastr);
		$todo->save();
		echo $data["echostr"];
	}
	else{
		return $this->view->render($response, "index.phtml", array(
			"currentTime" => new \DateTime(),
		));
	}
});

// 显示 todo 列表
$app->get('/todos', function(Request $request, Response $response) {
    $query = new Query("Todo");
    $query->descend("createdAt");
    try {
        $todos = $query->find();
    } catch (\Exception $ex) {
        error_log("Query todo failed!");
        $todos = array();
    }
    return $this->view->render($response, "todos.phtml", array(
        "title" => "TODO 列表",
        "todos" => $todos,
    ));
});

$app->post("/todos", function(Request $request, Response $response) {
    $data = $request->getParsedBody();
    $todo = new Object("Todo");
    $todo->set("content", $data["content"]);
    $todo->save();
    return $response->withStatus(302)->withHeader("Location", "/todos");
});

$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
});

$app->get('/wxauth', function (Request $request, Response $response) {
	$token="yuanzh";
	$data=$request->getQueryParams();
	//if(empty($data["signature"])){
	//	return;
	//}
	$signature=$data["signature"];
	$timestamp=$data["timestamp"];
	$nonce=$data["nonce"];
	$echostr=$data["echostr"];
	$array = array($token, $timestamp, $nonce);
	sort($array, SORT_STRING);
	$shastr=sha1(implode($array));
    $todo = new Object("WxRequest");
    $todo->set("signature", $signature);
    $todo->set("timestamp", $timestamp);
    $todo->set("nonce", $nonce);
    $todo->set("echostr", $echostr);
    $todo->set("shastr", $shastr);
    $todo->save();
	
	echo $data["echostr"];
});

$app->run();

